package acs.SmartSkip;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.util.Log;
import android.view.KeyEvent;

public class SmartSkip extends Activity {
	private long startTime = 0;
	private long endTime = 0;
    public TextView tv;
    private boolean running = false;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv = new TextView(this);
        tv.setText("Button Listen");
        setContentView(tv);
        Log.i("SmartSkip","Initialized");
        
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	if (keyCode == KeyEvent.KEYCODE_CAMERA && running == false)
    	{
    		running = true;
    		this.startTime = System.currentTimeMillis();
    		tv.setText("DOWN");
    		Log.i("SmartSkip", "Camera button pressed");
    	}
    	else if (keyCode == KeyEvent.KEYCODE_CAMERA && running)
    	{
    		
    		if ( 99 < ((System.currentTimeMillis() - this.startTime) / 10))
    		{
    			Log.i("SmartSkip","Camera button down >1 second");
    			if (this.endTime == 0)
    			{
    				Log.i("SmartSkip","Starting trackBack()");
    				this.endTime = System.currentTimeMillis();
    				trackBack();
    			}
    		}
    	}
    	else if (keyCode == KeyEvent.KEYCODE_BACK)
    	{
    		Log.i("SmartSkip","Exiting");
    		System.exit(0);
    	}
        return true;
      }
    
    public boolean onKeyUp(int keyCode, KeyEvent event) {
    	Log.i("SmartSkip","Key released");
    	if (keyCode == KeyEvent.KEYCODE_CAMERA)
    	{
    		Log.i("SmartSkip","Camera button released");
    		running = false;
    		if (this.endTime == 0)
    			this.endTime = System.currentTimeMillis();
    		
    			int time = (int)getElapsedTime();
    		this.endTime = 0;
    		if (time < 10)
    		{
    			Log.i("SmartSkip","Starting trackForward()");
    			trackForward();
    		}
    	}
        return true;
      }

    public long getElapsedTime() {
        long elapsed;
        elapsed = (this.endTime - this.startTime) / 100;
        
        return elapsed;
    }
    
    public void trackForward()
    {
    	Log.i("SmartSkip","trackForward()");
    	//Dialog d = new Dialog(this);
    	//d.setTitle("Track Forward");
    	//d.show();
    	tv.setText("Track Forward");
    }
    public void trackBack()
    {
    	Log.i("SmartSkip","trackBack()");
    //	Dialog d = new Dialog(this);
    //	d.setTitle("Track Back");
    //	d.show();
    	tv.setText("Track Back");
    }
    

}